/**
 * @file   Http.hpp
 * @author Liu Yuan <liuyuan@net.pku.edu.cn>
 * @date   Tue Jun 26 16:39:16 2007
 *
 * @brief The basic http operation
 *
 * Class http 
 */

#ifndef __HTTP_HPP
#define __HTTP_HPP

#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <string>

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include "Url.hpp"
#include "Page.hpp"

const char* const HTTP_VERSION                = "HTTP/1.0";
const int         DEFAULT_TIME_OUT            = 10;
const int         DEFAULT_REQUEST_BUFFER_SIZE = 1024;
const int         HEADER_BUF_SIZE             = 1024;
const int         DEFAULT_PAGE_SIZE           = 10240;

class Http
{
public:
    Http();

    bool FetchUrl(const Url& crUrl, Page& rPage);
    // 持续连接，最开始的时候可以传入-1
    // bool FetchUrlPersist(const Url& crUrl, Page &rPage, int &sock);
    void SetTimeOut(int timeOut);

private:
    int  MakeSocket(const std::string& crHost, int port);
    bool MakeRequest(int sock,
                     const std::string& crHost,
                     const std::string& crResPath, bool isPersist);

    int  ReadHeader(int sock, char *pHeadPtr);
    int  ReadBody(int sock, char *pBodyPtr, int bodyLen);

private:
    int mTimeOut;
};

#endif
